
define(['config/appregister','videoPlay'],function(app){
    app.directive("playvideo",function($sce){
    	
        return {
        	templateUrl:"app/business/vedio/dir/template/play.html",
        	scope:{
            	conf:"=need"
            },

            link:function(scope,element,attrs){
            	videojs.options.flash.swf = "video-js.swf";
            	$(document).ready(function(){
            			for(var i=0;i<scope.conf.videoSubTitleInfo.length;i++){
			            	var trackElement = document.createElement('track');
			            	trackElement.kind="subtitles";
			            		trackElement.src=scope.conf.videoSubTitleInfo[i].address;
			            		trackElement.label=scope.conf.videoSubTitleInfo[i].epDc.dictName;
			            		trackElement.srclang="ch-zn";
			            		$("#example_video_1").append(trackElement)
					   }
            			var player = videojs("example_video_1");	
						var setCookie = function setCookie(name, value, hours) {
							var d = new Date();
							d.setTime(d.getTime() + hours * 3600 * 1000);
							document.cookie = name + '=' + value + '; expires=' + d.toGMTString();
						}
				//setCookie("username",1212121,3);
				//设置Cookie
			var getCookie = function getCookie(name) {
					var arr = document.cookie.split('; ');
					for(var i = 0; i < arr.length; i++) {
						var temp = arr[i].split('=');
						if(temp[0] == name) {
							return temp[1];
						}
					}
					return '';
				}
				//getCookie("name")
				//删除Cookie
			var removeCookie = function removeCookie(name) {
				var d = new Date();
				d.setTime(d.getTime() - 10000);
				document.cookie = name + '=1; expires=' + d.toGMTString();
			}

//removeCookie("name")
//设置过期时间 3天
//设置页面刷新的Token
var setRefreshToken = function(refreshToken) {
		if(refreshToken != null && refreshToken != "") {
			setCookie("refreshToken", refreshToken, 1 * 60);
			return true;
		}
		return false;
	}
	//获得页面刷新的Token
var getRefreshToken = function() {
		var refreshToken = getCookie("refreshToken");
		if(refreshToken != null && refreshToken != "") {
			return refreshToken;
		}
		return false;
	}
	//设置追踪视频
var setVideoTrack = function(videoSourceId, currentTime) {
		var videoTrack = {

		};
		if(videoSourceId != null && currentTime != null) {
			var videoTrackInCookieSerializable = getCookie("videoTrack");
			var videoTrackInCookie = null;
			if(videoTrackInCookieSerializable == null || videoTrackInCookieSerializable == "") {
				videoTrack[videoSourceId] = currentTime;
				setCookie("videoTrack", JSON.stringify(videoTrack), 60 * 60 * 24 * 3);
			} else {
				videoTrackInCookie = JSON.parse(videoTrackInCookieSerializable);
				videoTrackInCookie[videoSourceId] = currentTime;
				setCookie("videoTrack", JSON.stringify(videoTrackInCookie), 60 * 60 * 24 * 3);
			}
			return true;
		}
		return false;
	}
	//获得视频追踪
var getVideoTrack = function() {
	var videoTrack = getCookie("videoTrack");
	if(videoTrack != null && videoTrack != "") {
		//		console.log(videoTrack);
		return JSON.parse(videoTrack);
	};
	return null;
}

var setAuth = function(seed, authToken) {
	var auth = {

	};
	if(seed != null && seed != "" && authToken != null && authToken != "") {
		auth.seed = seed;
		auth.authToken = authToken;
		setCookie("auth", JSON.stringify(auth), 60 * 60 * 24 * 3);
		return true;
	};
	return false;
}

var getAuth = function() {
	var auth = getCookie("auth");
	if(auth != null && auth != "") {
		return JSON.parse(auth);
	}
	return null;
}
				//模拟取得视频源数据
			/*	console.log(scope.conf)*/
				var videoSourceId = scope.conf.videoid;
				//刷新进度跟踪的时间间隔
				var refreshTrackInterval = 1000;
				//刷新Cookie进度的方法
				var refreshTrack = function() {
					if(player != null) {
						var currentTime = player.currentTime();
						//以视频源ID为索引 为videoTack数组赋予时间
						setVideoTrack(videoSourceId, currentTime);
												/*console.log('currentTime=' + currentTime);
												console.log('videoTrack=' + getVideoTrack("videoTrack")[videoSourceId]);*/
					}

				}

				//存储Cookie中的videoTrack对象
				var videoTrack = {}
					//先定义为空
var initPlayer = function() {
					//在初始化视频组件后赋值
				/*	console.log(player.currentTime());*/
					//获取Cookie中的视频进度 并在视频组件初始化后跳转进度
				/*	console.log(getVideoTrack("videoTrack"));*/
					var videoTrack = getVideoTrack("videoTrack");
			/*		console.log(videoTrack);*/
					var trackTime = null;
					if(videoTrack != null) {
						trackTime = videoTrack[videoSourceId];
						//						console.log(trackTime);
						if(trackTime != undefined || trackTime != null) {
							player.currentTime(trackTime);
						}
					}
					//					function轮流执行
					setInterval(refreshTrack, refreshTrackInterval);
				};

			player.ready(function(){
						 player.play();
						 scope.videoURL = $sce.trustAsResourceUrl(scope.conf.videoUrl)
		            	 scope.track = scope.conf.videoSubTitleInfo;
					    initPlayer();
					    
					})
					
					
            	})
            
    				
					
					
    				

				
				
            }
        }
    });
});
